<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

/**
 * Class Cards
 *
 * @package App\Model
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Cards onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Cards withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Cards withoutTrashed()
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards query()
 * @property int $id 主键
 * @property int $user_id 用户user_id
 * @property string $head_picture 用户头像
 * @property string $logo logo
 * @property string $user_name 用户名
 * @property string $motto 自我描述、人生格言、座右铭等
 * @property string $mobile_phone 手机号
 * @property string $telephone 座机电话
 * @property string $company 公司
 * @property string $industry 行业
 * @property string $profession 职业
 * @property int $sex 性别1男2女
 * @property string|null $birthday 生日
 * @property string $wechat_code 微信号
 * @property string $qq qq号
 * @property string $email email
 * @property string $website 网址
 * @property string $province 省
 * @property string $city 市
 * @property string $area 区
 * @property string $address_detail 详细地址
 * @property int $popularity 人气
 * @property int $saved 保存
 * @property string|null $style 样式
 * @property string|null $introduce 个人介绍
 * @property string|null $album
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 创建时间
 * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereAddressDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereAlbum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereBirthday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereHeadPicture($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereIndustry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereIntroduce($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereMobilePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereMotto($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards wherePopularity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereProfession($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereQq($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereSaved($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereStyle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereUserName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereWebsite($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereWechatCode($value)
 * @property-read mixed $address
 * @property int $open_phone
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereOpenPhone($value)
 * @property int $is_show_phone 是否显示手机号码，0不显示，1显示
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereIsShowPhone($value)
 * @property int $share 分享量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Cards whereShare($value)
 */
class Cards extends Model
{
    use SoftDeletes;
    protected $table = 'cards';
    protected $primaryKey = 'id';
    protected $dates = ['deleted_at'];

    protected $fillable = ['user_id',
        'user_name','head_picture','mobile_phone','telephone',
        'company','industry','sex','birthday','profession',
        'wechat_code','qq','email','website','province','city','area',
        'address_detail', 'motto', 'logo', 'style','introduce', 'album'];

    protected $appends = ['address'];

    public function getAddressAttribute()
    {
        $province = '';
        $city = '';
        $area = '';
        if (!empty($this->province)){
            $province = Places::getPlacesName($this->province);
        }
        if (!empty($this->city)){
            $city = Places::getPlacesName($this->city);
        }
        if (!empty($this->area)){
            $area = Places::getPlacesName($this->area);
        }

        return trim($province .' '. $city . ' ' . $area);
    }

    public function add(Request $request)
    {
        $data = $request->input();

        $this->user_id = $request->user()->id;
        $this->user_name = $data['user_name'];
        $this->head_picture = $data['head_picture'];
        $this->mobile_phone = $data['mobile_phone'];
        $this->telephone = $data['telephone'];
        $this->company = $data['company'];
        $this->industry = $data['industry'];
        $this->profession = $data['profession'];
        $this->wechat_code = $data['wechat_code'];
        $this->qq = $data['qq'];
        $this->email = $data['email'];
        $this->website = $data['website'];
        $this->province = $data['province'];
        $this->city = $data['city'];
        $this->area = $data['area'];
        $this->address_detail = $data['address_detail'];
        return $this->save();
    }

}
